//
//  TabBarController.m
//  Golf
//
//  Created by 周成龙 on 16/11/2.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "TabBarController.h"

#import "ZConstant.h"
#import "UIImage+image.h"

#import "SquareViewController.h"
#import "MessageViewController.h"
#import "ServiceViewController.h"
#import "ProfileViewController.h"
#import "NullViewController.h"

#import "PublishViewController.h"

#import "ZNavigationController.h"
#import "ComposeView.h"

@interface TabBarController ()

@property (nonatomic, strong)UIButton *composeButton;
@property (nonatomic, strong)ComposeView *composeView;

@end

@implementation TabBarController

- (UIButton *)composeButton {
    if (_composeButton == nil) {
        _composeButton = [[UIButton alloc] init];
        [_composeButton setImage:[UIImage imageNamed:kTbabarImageName(@"图层-1758")] forState:UIControlStateNormal];
        [_composeButton addTarget:self action:@selector(composeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _composeButton;
}

- (ComposeView *)composeView {
    if (_composeView == nil) {
        _composeView = [[ComposeView alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticeDismissView) name:@"kNoticeDismissView" object:nil];
        kWeakSelf
        _composeView.composeBlock = ^(NSInteger index){
            [weakSelf dismiss];
            
            switch (index) {
                case 1: {
                    NSLog(@"场地查询");
                }
                    break;
                    
                case 2: {
                    NSLog(@"约球");
                }
                    break;
                    
                case 3: {
                    NSLog(@"发布");
                }
                    break;
                    
                default:
                    break;
            }
            
        };
    }
    return _composeView;
}

+ (void)initialize {
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    NSMutableDictionary *attNor = [NSMutableDictionary dictionary];
    attNor[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.25 green:0.77 blue:0.59 alpha:1.00];
    [item setTitleTextAttributes:attNor forState:UIControlStateSelected];
    
    NSMutableDictionary *attSel = [NSMutableDictionary dictionary];
    attSel[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.53 green:0.53 blue:0.54 alpha:1.00];
    [item setTitleTextAttributes:attSel forState:UIControlStateNormal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpAllChildViewController];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    CGFloat width = self.tabBar.width / self.tabBar.items.count;
    self.composeButton.frame = CGRectMake(width*2, kScreenHeight - self.tabBar.height, width, self.tabBar.height);
    //    [self.tabBar.superview addSubview:self.composeButton];
    [self.tabBar addSubview:self.composeButton];
}

- (void)setUpAllChildViewController {
    SquareViewController *squarevc = [[SquareViewController alloc] init];
    ZNavigationController *sqnav = [[ZNavigationController alloc] initWithRootViewController:squarevc];
    [self setOneChildViewController:sqnav image:[UIImage imageNamed:kTbabarImageName(@"资讯未中icon-拷贝-2")] selectedImage:[UIImage imageWithOriginalName:kTbabarImageName(@"资讯选中icon")] title:@"广场"];
    
    MessageViewController *messvc = [[MessageViewController alloc] init];
    ZNavigationController *messnav = [[ZNavigationController alloc] initWithRootViewController:messvc];
    [self setOneChildViewController:messnav image:[UIImage imageNamed:kTbabarImageName(@"消息未选中icon")] selectedImage:[UIImage imageWithOriginalName:kTbabarImageName(@"消息选中icon")] title:@"消息"];
    
    NullViewController *nuvc = [[NullViewController alloc] init];
    [self setOneChildViewController:nuvc image:[UIImage imageNamed:kTbabarImageName(@"")] selectedImage:[UIImage imageWithOriginalName:kTbabarImageName(@"")] title:@""];
    
    ServiceViewController *sevc = [[ServiceViewController alloc] init];
    ZNavigationController *senav = [[ZNavigationController alloc] initWithRootViewController:sevc];
    [self setOneChildViewController:senav image:[UIImage imageNamed:kTbabarImageName(@"服务未选中icon")] selectedImage:[UIImage imageWithOriginalName:kTbabarImageName(@"服务选中icon")] title:@"服务"];
    
    ProfileViewController *mvc = [[ProfileViewController alloc] init];
    ZNavigationController *mnav = [[ZNavigationController alloc] initWithRootViewController:mvc];
    
    [self setOneChildViewController:mnav image:[UIImage imageNamed:kTbabarImageName(@"用户中心未选中icon")] selectedImage:[UIImage imageWithOriginalName:kTbabarImageName(@"用户中心选中icon")] title:@"我的"];
}

- (void)setOneChildViewController:(UIViewController *)vc image:(UIImage*)image selectedImage:(UIImage*)selectedImage title:(NSString*)title
{
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    [self addChildViewController:vc];
}

- (void)composeButtonClick:(UIButton *)sender {
    
    if (_composeView) {
        [self dismiss];
    }else {
        self.composeView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [self.composeView showInView:sender.superview belowSubview:sender];
        [self addAnimationRight:YES];
    }
}

- (void)addAnimationRight:(BOOL)isRight {
    CGFloat angle = isRight ? M_PI_4:(0);
    [UIView animateWithDuration:0.35 animations:^{
        self.composeButton.transform = CGAffineTransformMakeRotation(angle);
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
}

- (void)dismiss {
    int count = (int)self.composeView.subviews.count;
    for (int i = 0; i < count; i ++){
        UIButton *btn = (UIButton *)[self.composeView viewWithTag:i+1];
        [UIView animateWithDuration:0.35 animations:^{
            btn.center = CGPointMake(kScreenWidth*0.5, kScreenHeight-49*0.5);
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            btn.hidden = YES;
            [btn removeFromSuperview];
            MyLog(@"-->%d", i);
            [self.composeView removeFromSuperview];
            self.composeView = nil;
            [[NSNotificationCenter defaultCenter] removeObserver:self];
        }];
    }
    [self addAnimationRight:NO];
    
}

- (void)noticeDismissView {
    [self dismiss];
}

- (void)dealloc {
    NSLog(@"%s", __func__);
    
}


@end
